package ru.tennisworld.match.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.client.ExpectedCount;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.test.web.client.match.MockRestRequestMatchers;
import org.springframework.test.web.client.response.MockRestResponseCreators;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.client.RestTemplate;
import ru.tennisworld.match.dto.integration.UserReferenceDto;
import ru.tennisworld.match.dto.integration.UserUpdateRatingRequestDto;

import java.io.File;
import java.util.List;

import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;

@SpringBootTest(
        webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@AutoConfigureMockMvc
@TestPropertySource(locations = "classpath:application.yml")
public class IntegrationUtilsTest {

    @Value("${user-service.url}")
    private String userServiceUrl;

    @Value("${user-service.path.update-rating}")
    private String userUpdateRatingPath;

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private IntegrationClient integrationClient;

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Test
    void updateMatchRequestTest() throws Exception {
        final File requestFile = new File("src/test/java/ru/tennisworld/match/utils/data/UpdateUserRatingRequest.json");
        UserUpdateRatingRequestDto request = objectMapper.readValue(requestFile, UserUpdateRatingRequestDto.class);
        RestTemplate restTemplate = new RestTemplate();
        MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
        mockServer.expect(ExpectedCount.once(),
                MockRestRequestMatchers.requestTo(userServiceUrl + userUpdateRatingPath))
                .andExpect(MockRestRequestMatchers.method(HttpMethod.PUT))
                .andRespond(MockRestResponseCreators.withStatus(HttpStatus.OK)
                        .contentType(MediaType.APPLICATION_JSON)
                .body(objectMapper.writeValueAsString(request)));
    }

    @Test
    void getUserReferenceList() {
        List<String> requestUserList = List.of("lisic4kin", "naymovich");
        List<UserReferenceDto> userList = integrationClient.getUserReferenceList(requestUserList);
        Assertions.assertEquals(requestUserList.size(), userList.size());
    }
}
